package com.app.paginglearn

import android.content.Context
import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.databinding.DataBindingUtil
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProviders
import androidx.paging.PagedListAdapter
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.alibaba.android.arouter.facade.annotation.Route
import com.app.common.activity.BaseActivity
import com.app.datamanagermodule.entity.User
import com.app.paginglearn.databinding.ActivityPagingLearnBinding
import com.app.paginglearn.databinding.ActivityPagingLearnItemBinding
import com.app.paginglearn.model.PagingLearnModel


@Route(path = "/paging_learn/paging_learn_activity")
class PagingLearnActivity : BaseActivity() {
    private val TAG = PagingLearnActivity::class.java.simpleName
    private lateinit var viewModel: PagingLearnModel

    //private val disposable = CompositeDisposable()

    private lateinit var activityPagingLearnBinding: ActivityPagingLearnBinding

    private lateinit var adapter: PagingLearnListAdapter

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        viewModel = ViewModelProviders.of(this).get(PagingLearnModel::class.java)

        activityPagingLearnBinding = DataBindingUtil.setContentView(this@PagingLearnActivity, R.layout.activity_paging_learn)

        adapter = PagingLearnListAdapter(this@PagingLearnActivity)

        viewModel.userList2.observe(this, Observer {
            Log.d(TAG, "it -> ${it.size}")
            adapter.submitList(it)
        })
        activityPagingLearnBinding.activityPagingLearnList.layoutManager = LinearLayoutManager(this)
        activityPagingLearnBinding.activityPagingLearnList.adapter = adapter


    }

}

class PagingLearnListAdapter(private val context: Context): PagedListAdapter<User, PagingLearnListAdapter.ViewHolder>(DIFF_CALLBACK) {
    private val TAG = PagingLearnListAdapter::class.java.simpleName

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val viewDataBinding = DataBindingUtil.inflate<ActivityPagingLearnItemBinding>(LayoutInflater.from(context), R.layout.activity_paging_learn_item, parent, false)

        return ViewHolder(viewDataBinding)
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        val user = getItem(position)
        Log.d(TAG, "PagingLearnListAdapter -> user -> $user")
        holder.activityPagingLearnItemBinding.setVariable(BR.user, user)

    }

    companion object {
        private val DIFF_CALLBACK = object :
                DiffUtil.ItemCallback<User>() {
            // Concert details may have changed if reloaded from the database,
            // but ID is fixed.
            override fun areItemsTheSame(oldUser: User,
                                         newUser: User) = oldUser.id == newUser.id

            override fun areContentsTheSame(oldUser: User,
                                            newUser: User) = oldUser == newUser
        }
    }


    inner class ViewHolder(val activityPagingLearnItemBinding: ActivityPagingLearnItemBinding) : RecyclerView.ViewHolder(activityPagingLearnItemBinding.root)
}